package com.example.campuslink.ui.fragment

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.adpter.MessageListAdapter
import com.example.campuslink.commen.adpter.SchoolUserListAdapter
import com.example.campuslink.commen.tools.SpacesItemButtonDecoration
import com.example.campuslink.databinding.FragmentMessageBinding
import com.example.campuslink.model.MessageTypeData
import com.example.campuslink.viewModel.MessageViewModel

class MessageFragment : BaseFragment<FragmentMessageBinding, MessageViewModel>(
    MessageViewModel::class.java,
    R.layout.fragment_message,
    BR.viewModel
) {
    private var adapter: SchoolUserListAdapter? = null
    private var messageAdapter: MessageListAdapter? = null
    private val messageList: MutableList<MessageTypeData> = mutableListOf()
    override fun onClick() {

    }

    override fun initViews() {
        adapter = SchoolUserListAdapter(mViewModel!!)
        mViewModel?.schoolUserNumber?.observe(this) {
            if (it != null) {
                mViewDataBinding?.tvNoMoreUser?.visibility = if (it.isEmpty()) {
                    View.VISIBLE
                } else {
                    View.GONE
                }
                adapter?.setListData(it)
            }
        }
        mViewDataBinding?.rvUserList?.adapter = adapter
        mViewDataBinding?.rvUserList?.layoutManager = LinearLayoutManager(requireContext())
        mViewDataBinding?.rvUserList?.addItemDecoration(SpacesItemButtonDecoration(15))
        mViewDataBinding?.freshLayout?.setOnRefreshListener {
            mViewModel?.getNowSchoolNumberList()
            mViewDataBinding?.freshLayout?.isRefreshing = false
        }
        mViewDataBinding?.rvMessageList?.adapter = messageAdapter
        mViewDataBinding?.rvMessageList?.layoutManager = GridLayoutManager(requireContext(), 3)
    }

    override fun initData() {
        mViewModel?.context = requireContext()
        mViewModel?.initDB(requireContext())
        mViewModel?.getNowSchoolNumberList()
        messageAdapter = MessageListAdapter(mViewModel!!)
        messageList.add(MessageTypeData("赞", R.drawable.love))
        messageList.add(MessageTypeData("新增关注", R.drawable.user))
        messageList.add(MessageTypeData("评论和@", R.drawable.context))
        messageAdapter?.setListData(messageList)
    }

}